/*
 * motor_dirver.h
 *
 *  Created on: 2021年10月31日
 *      Author: 王慧云
 */

#ifndef BSP_MOTOR_DIRVER_H_
#define BSP_MOTOR_DIRVER_H_

#include <stdio.h>
#include "main.h"
#include "tim.h"
#include <stdbool.h>

/**宏定义 定义各种控制方式**/
#define KNOB       0//此时输入为表盘旋钮
#define IO         1//此时输入为IO外部信号
#define RS         2//此时输入为外部串行信号
#define PID_INPUT_KNOB    3 //采用PID控制，此时给定为表盘旋钮
#define ANALOG            4 //此时给定为外部模拟输入

#define CW  0 //正转
#define CCW 1 //反转

typedef struct
{
    float target_val;
    //float actual_val;
    float err;
    float err_next;
    float err_last;
    float Kp;
    float Ki;
    float Kd;
}_pid;

//电机的频率
extern uint16_t motor_freq; 
//函数功能获得当前频率   
#define motor_get_freq()  (motor_freq) 

//电机的占空比
extern uint16_t motor_dutyCycle;
//函数功能获得当前占空比
#define motor_get_dutyCycle() (motor_dutyCycle)

//电机最大速度
extern uint16_t motor_maxSpeed; 
//设置最大速度
#define motor_set_maxSpeed(x) (motor_maxSpeed = x) 
#define motor_get_maxSpeed()  (motor_maxSpeed)


//电机运行模式 0--未运行 1--运行  2--故障
extern uint8_t motor_runStatus; 
//函数功能获得电机运行状态
#define motor_get_runStatus()  (motor_runStatus) 

extern bool motor_runDir;   //电机运行模式 0--正转  1--反转
#define motor_set_RunDir(x)   (motor_runDir = x)
#define motor_get_runDir()    (motor_runDir)

void motor_set_freq(uint16_t freq); // 设置电机频率
void motor_set_dutyCycle(uint16_t duty,bool dir); //设置电机占空比
void motor_set_RunStatus(uint8_t status); //设置电机运行状态
void motor_set_speed(int16_t speed);

/*电机调节方式设置*/
extern uint8_t motor_CtrlMode; 
//设置控制方式
#define motor_set_CtrlMode(x)  {if(x<=4) motor_CtrlMode = x;}
//得到控制方式
#define motor_get_CtrlMode()   (motor_CtrlMode) 

void motor_speed_tick(uint16_t tick_period);

extern uint16_t encoder_resolution; //编码器分辨率
extern uint16_t reduction_ratio; //减速比
extern float motor_speed; //电机速度
#define motor_set_encoderResolution(x) (encoder_resolution = x)//设置编码器频率
#define motor_get_encoderResolution()  (encoder_resolution)    //获取编码器频率
#define motor_set_reductionRatio(x)    (reduction_ratio = x)   //设置电机减速比
#define motor_get_reductionRatio(x)    (reduction_ratio)       //获取电机减速比
#define motor_get_speed()              (motor_speed)           //获得电机速度

extern bool pid_enable; //pid控制使能 0--不使能 1--使能
#define motor_set_PIDenable(x)  (pid_enable = x)//使能pid控制
#define motor_IsPIDenable()     (pid_enable)//判断pid是否使能


extern _pid pid;
float PID_realize(float actual_val); //PID调节
#define motor_set_pid_Kp(x)     (pid.Kp = x)
#define motor_get_pid_Kp()      (pid.Kp)

#define motor_set_pid_Ki(x)     (pid.Ki = x)
#define motor_get_pid_Ki()      (pid.Ki)

#define motor_set_pid_Kd(x)     (pid.Kd = x)
#define motor_get_pid_Kd()      (pid.Kd)

#define motor_set_pid_target(x) (pid.target_val = x)
#define motor_get_pid_target()  (pid.target_val)

extern int io_speed1; //在IO模式下，速度1
extern int io_speed2; //在IO模式下，速度2
extern int io_speed3; //在IO模式下，速度3
extern int io_speed4; //在IO模式下，速度4

#define motor_set_io_speed1(x) (io_speed1 = x)
#define motor_set_io_speed2(x) (io_speed2 = x)
#define motor_set_io_speed3(x) (io_speed3 = x)
#define motor_set_io_speed4(x) (io_speed4 = x)
#define motor_get_io_speed1()  (io_speed1)
#define motor_get_io_speed2()  (io_speed2)
#define motor_get_io_speed3()  (io_speed3)
#define motor_get_io_speed4()  (io_speed4)
void motor_io_set_speed(uint8_t mode);


extern bool motor_isSpeedInput; //判断是否是以转速方式调节的
#define motor_set_SpeedInputMode(x)  (motor_isSpeedInput = x)
#define motor_get_SpeedInputMode()   (motor_isSpeedInput)

#endif /* BSP_MOTOR_DIRVER_H_ */
